﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Business;
using Business.DataBusiness;

namespace MuaBanDT
{
    public partial class BaoHanh : Form
    {
      Utilities getData = new Utilities();
      ThongTinBaoHanh data = new ThongTinBaoHanh();
        public BaoHanh()
        {
            InitializeComponent();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.txtmaBH.Text = "";
            this.txtTenKH.Text = "";
            this.txtIMEI.Text = "";
            this.dtNgayNhan.Value = DateTime.Now;
            this.dtNgayTra.Value = DateTime.Now;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ThongTinBaoHanh a = new ThongTinBaoHanh();
            a.MaBH= getData.CreateID("ThongTinBaoHanh","BH");
            a.TenKH=this.txtTenKH.Text;
            a.IMEI=this.txtIMEI.Text;
            a.NgayNhan= DateTime.Parse(this.dtNgayNhan.Value.ToShortDateString());
            a.NgayTra = DateTime.Parse(this.dtNgayTra.Value.ToShortDateString());
            this.txtmaBH.Text = a.MaBH;
          
            if (a.Insert()==1)
            {
                MessageBox.Show("Lưu thành công", "Thông Tin Bảo Hành", MessageBoxButtons.OK);
                BaoHanh_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Lỗi xảy ra,bạn vui lòng kiểm tra lại", "Thông Tin Bảo Hành", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
         }
        private void ViewThongTinBH_Load()
        {
            dataGridViewBaoHanh.DataSource = getData.GetDataTable("ThongTinBaoHanh");  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ChiTietHDB a = new ChiTietHDB();

            if (a.TestIMEI(this.txtIMEI.Text) == 0)
                {
                    MessageBox.Show("IMEI không tồn tại trong danh sách hàng đã bán ","IMEI",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    this.txtIMEI.Text = "";
                }
           else
                MessageBox.Show("Ngày mua : " + getData.NgayBan(this.txtIMEI.Text)+ "\n" + "Thời gian bảo hành : " + getData.ThoiGianBaoHanh(this.txtIMEI.Text), "IMEI", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        private void BaoHanh_Load(object sender, EventArgs e)
        {
            dataGridViewBaoHanh.DataSource = getData.GetDataTable("ThongTinBaoHanh");
        }

        private void buttonCapNhat_Click(object sender, EventArgs e)
        {
            ThongTinBaoHanh a = new ThongTinBaoHanh();
            a.MaBH = this.txtmaBH.Text;
            a.TenKH = this.txtTenKH.Text;
            a.IMEI = this.txtIMEI.Text;
            a.NgayNhan = DateTime.Parse(this.dtNgayNhan.Value.ToShortDateString());
            a.NgayTra = DateTime.Parse(this.dtNgayTra.Value.ToShortDateString());
            a.Update();
            this.dataGridViewBaoHanh.DataSource = getData.GetDataTable("ThongTinBaoHanh");
        }

        private void dataGridViewBaoHanh_RowEnter(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                this.txtmaBH.Text = getData.Value("ThongTinBaoHanh", e.RowIndex, 0);
                this.txtTenKH.Text = getData.Value("ThongTinBaoHanh", e.RowIndex, 1);
                this.txtIMEI.Text = getData.Value("ThongTinBaoHanh", e.RowIndex, 2);
                this.dtNgayNhan.Text = getData.Value("ThongTinBaoHanh", e.RowIndex, 3);
                this.dtNgayTra.Text = getData.Value("ThongTinBaoHanh", e.RowIndex, 4);
            }
            catch 
            {
                this.txtmaBH.Text = "";
                this.txtTenKH.Text = "";
                this.txtIMEI.Text = "";
                this.dtNgayNhan.Value = DateTime.Now;
                this.dtNgayTra.Value = DateTime.Now;
            }
        }

     }
}
